<?php


function getTasklists($con, $db_name, $requestVars,$sessionEnabled,$type)
{
    $pid = $requestVars['pid'];
    $tlid = $requestVars['tlid'];
    
    $status = $requestVars['status'];
    $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    } 
    $query = "SELECT * FROM $db_name.tasklist ";
    
    if($pid)
    {
        $query = "SELECT * FROM $db_name.tasklist  
            WHERE project = $pid";
    }
    
    if($tlid)
    {
         $query = "SELECT * FROM $db_name.tasklist  
            WHERE ID = $tlid";
    }
   
    if($status)
    {
        $query = $query." and status = $status";
    }
     $result = mysqli_query($con,$query);

    if(!$result)
    {
        die("mysql error: ".mysql_error()."[$query]");
    }
    $TasklistArray = array();
    $counter = 0;
    while($row = mysqli_fetch_assoc($result))
    {

     $id = $row['ID'];
     $pid = $row['project'];     
      $mid = $row['milestone'];
      
     $name = $row['name'];
     $desc = $row['desc'];
     $start = $row['start'];
     $status = $row['status'];
     $access = $row['access'];
    



     $Tasklist =  new Tasklist($id, $pid, $name, $desc, $start,$status,$access,$mid);

     $TasklistArray[$counter] = $Tasklist;
     $counter++;

    }
 
    if($tlid)
    {
        if($type == 'json')
        {
            return TasklistResponseJSON(1,"Success!",$id,$pid, $mid, $name, $desc, $start,$status,$access);
        }
        else if($type == 'xml')
        {
            return TasklistResponseXML(1,"Success!",$id,$pid, $mid, $name, $desc, $start,$status,$access);
        }        
    }
    else
    {
        if($type == 'json')
        {
            return ArrayTasklistResponseJSON(1,"Success!",$TasklistArray);
        }
        else if($type == 'xml')
        {
            return ArrayTasklistResponseXML(1,"Success!",$TasklistArray);
        }        
    }       
}

function addTasklist($con, $db_name, $requestVars,$sessionEnabled, $type)
{
     $pid = $requestVars['pid'];
     
    if(!$pid)
    {
        if($type == 'json')
        {
            return GenericResponseJSON(7, "You must pass a project id (pid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(7, "You must pass a project id (pid) !");
        }
        
    }
    
    $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    } 
     
     $name = $requestVars['name'];
     $desc = $requestVars['desc'];
     $access = $requestVars['access'];
     $mid = $requestVars['mid'];
     if(!$milestone)
     {
         $milestone = 0;
     }
     if(!$access)
     {
         $access = 0;
     }

    $name = mysqli_real_escape_string($con,$name);
    $desc = mysqli_real_escape_string($con,$desc);
    $pid = (int) $pid;
    $access = (int) $access;
    $mid = (int) $mid;

    $start = time();

    $ins = mysqli_query($con,"INSERT INTO $db_name.tasklist (`project`,`name`,`desc`,`start`,`status`,`access`,`milestone`) VALUES ($pid,'$name','$desc','$start',1,$access,$mid)");
    
    $tlid = mysqli_insert_id($con);
    
    $query  = "SELECT * From $db_name.tasklist WHERE id = $tlid";
    $result = mysqli_query($con,$query);

    if(!$result)
    {
        die("mysql error: ".mysql_error()."[$query]");
    }

    while($row = mysqli_fetch_assoc($result))
    {
        $tlid = $row['ID'];        
        $name = $row['name'];
        $desc = $row['desc'];
        $start = $row['start'];          
        $status = $row['status'];
        $access = $row['access'];
        $mid = $row['milestone'];
      
    }
    
    
    if ($ins)
    {     
        if($type == 'json')
        {            
            return TasklistResponseJSON(1, "Success!", $tlid,$pid,$mid,$name,$desc,$start,$status,$access);
        }
        else if($type == 'xml')
        {
            return TasklistResponseXML(1, "Success!", $tlid,$pid,$mid,$name,$desc,$start,$status,$access);
        }           
        
    }
    else
    {
        if($type == 'json')
        {            
            return GenericResponseJSON(32,"Failed to create tasklist, please try again later");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(32,"Failed to create tasklist, please try again later");
        }       
    }
}

function editTasklist($con, $db_name, $requestVars,$sessionEnabled, $type)
{
    $tlid = $requestVars['tlid'];
    if(!$tlid)
    { 
        if($type == 'json')
        {
            return GenericResponseJSON(9, "You must pass a tasklist id (tlid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(9, "You must pass a tasklist id (tlid) !");
        }        
    }
    
    $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    }     


    $name = $requestVars['name'];
    $desc = $requestVars['desc'];
    $mid = $requestVars['mid'];

    $get ="select * from $db_name.tasklist WHERE ID=$tlid";
    $result =  mysqli_query($con,$get);

    if(!$result)
    {
        die("mysql error: ".mysql_error()."[$get]");
    }
    while($row = mysqli_fetch_assoc($result))
    {
        $pid = $row['project'];
        $start = $row['start'];
        $status = $row['status'];
        $access = $row['access'];
        
        if(!$mid)
        {
            $mid = $row['milestone'];
            
        }
        if(!$name)
        {

            $name = $row['name'];                    
        }
        else
        {
            $name = mysqli_real_escape_string($con,$name);
        }

        if(!$desc)
        {
            $desc = $row['name'];
        }
        else
        {
            $desc = mysqli_real_escape_string($con,$desc);
        }     
       
    }

    $tlid = (int) $tlid;
    $mid = (int) $mid;

    $upd = mysqli_query($con,"UPDATE $db_name.tasklist SET `name`='$name', `desc`='$desc', `milestone`=$mid WHERE ID = $tlid");
    if ($upd)
    {                
        if($type == 'json')
        {            
            return TasklistResponseJSON(1, "Success!", $tlid,$pid,$mid,$name,$desc,$start,$status,$access);
        }
        else if($type == 'xml')
        {
            return TasklistResponseXML(1, "Success!", $tlid,$pid,$mid,$name,$desc,$start,$status,$access);
        }         
        
    }
    else
    {
        if($type == 'json')
        {            
            return GenericResponseJSON(16,"Failed to update tasklist, please try again later");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(16,"Failed to update tasklist, please try again later");
        }           

    }
}



function delTasklist($con, $db_name, $requestVars,$sessionEnabled, $type)
{
    $tlid = $requestVars['tlid'];
    if(!$tlid)
    { 
        if($type == 'json')
        {
            return GenericResponseJSON(9, "You must pass a tasklist id (tlid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(9, "You must pass a tasklist id (tlid) !");
        }        
    }
    
    $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    }     
    
    $del = mysqli_query($con,"DELETE FROM $db_name.tasklist WHERE ID = $tlid LIMIT 1");

    if ($del)
    {
        // Delete the Assigned tasks first
        $get = "SELECT * FROM $db_name.tasks where liste=$tlid";
        $result =  mysqli_query($con,$get);

        if(!$result)
        {
            die("mysql error: ".mysql_error()."[$get]");
        }
        while($row = mysqli_fetch_assoc($result))
        {
            mysqli_query($con,"DELETE FROM $db_name.tasks_assigned where task=".$row['ID']);            

        }                     

        // Then the regular tasks 
         mysqli_query($con,"DELETE FROM $db_name.tasks where liste= $tlid");            
         if($type == 'json')
        {
             return GenericResponseJSON(1, "Success!");
        }
        else if($type == 'xml')
        {
             return GenericResponseXML(1, "Success!");
        }       
    }
    else 
    {        
        if($type == 'json')
        {
            return GenericResponseJSON(20, "Failed to delete tasklist, please try again later");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(20, "Failed to delete tasklist, please try again later ");
        }
    }
}
function closeTasklist($con, $db_name, $requestVars,$sessionEnabled,$type)
{
    $tlid = $requestVars['tlid'];
    if(!$tlid)
    { 
        if($type == 'json')
        {
            return GenericResponseJSON(9, "You must pass a tasklist id (tlid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(9, "You must pass a tasklist id (tlid) !");
        }        
    }
    
    $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    }       
    $tlid = (int) $tlid;

    $upd = mysqli_query($con,"UPDATE $db_name.tasklist SET status = 0 WHERE ID = $tlid");

    // Close assigned milestone too, if no other open tasklists are assigned to it
    $sql = mysqli_query($con,"SELECT milestone FROM $db_name.tasklist WHERE ID = $tlid");
    $milestone = mysqli_fetch_row($sql);
    if ($milestone[0] > 0)
    {
        $sql2 = mysqli_query($con,"SELECT count(*) FROM $db_name.tasklist WHERE milestone = $milestone[0] AND status = 1");
        $cou = mysqli_fetch_row($sql2);

        if ($cou[0] == 0)
        {
            // Close milestone.
           $requestVars['mid'] = $milestone[0];
           $requestVars['ignoreTasklist'] = 1; // To prevent insane looping. (new version should be more efficient.)
           $milestoneResponse = closeMilestone($con, $db_name,$requestVars,$sessionEnabled);
        }
    }
    $get ="select * from $db_name.tasks WHERE liste=$tlid";
    $result =  mysqli_query($con,$get);

    if(!$result)
    {
        die("mysql error: ".mysql_error()."[$get]");
    }
    
    while($row = mysqli_fetch_assoc($result))
    {
        $row['tid'] = $row['ID'];
         closeTask($con,$db_name,$row,$sessionEnabled); 
        
    }

    // Log entry
    if ($upd) 
    {
        if($type == 'json')
        {
             return GenericResponseJSON(1, "Success!");
        }
        else if($type == 'xml')
        {
             return GenericResponseXML(1, "Success!");
        }       
    }
    else 
    {        
        if($type == 'json')
        {
            return GenericResponseJSON(24, "Failed to close tasklist, please try again later");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(24, "Failed to close tasklist, please try again later");
        }
         
    }      
}

function openTasklist($con, $db_name, $requestVars,$sessionEnabled)
{
    $tlid = $requestVars['tlid'];
    if(!$tlid)
    { 
        if($type == 'json')
        {
            return GenericResponseJSON(9, "You must pass a tasklist id (tlid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(9, "You must pass a tasklist id (tlid) !");
        }        
    }
    
    $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    } $id = (int) $id;

    $upd = mysqli_query($con,"UPDATE $db_name.tasklist SET status = 1 WHERE ID = $id");

    if ($upd)
    {

        return GenericResponseJSON(2, "Success!");
    }
    else
    {
        return GenericResponseJSON(5, "Failed to open tasklist!");
    }  
    
    $id = (int) $id;

    $upd = mysqli_query($con,"UPDATE $db_name.tasklist SET status = 1 WHERE ID = $id");

     if ($upd) 
    {
        if($type == 'json')
        {
             return GenericResponseJSON(1, "Success!");
        }
        else if($type == 'xml')
        {
             return GenericResponseXML(1, "Success!");
        }       
    }
    else 
    {        
        if($type == 'json')
        {
            return GenericResponseJSON(28, "Failed to open tasklist, please try again later");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(28, "Failed to open tasklist, please try again later");
        }
         
    }      
}
?>
